﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ParangonBiz.Data;

namespace ParangonBiz.AdminPanel
{
    public partial class Options : System.Web.UI.Page
    {
        private void FillContentTypeList()
        {
            List<SiteContentType> sctList = SiteContentType.GetAllContentTypes();
            if (sctList != null)
            {
                sctList.Insert(0, new SiteContentType()
                {
                    Title = "[-Выберите список, являющийся новостями-]",
                    SiteContentTypeId = null
                });

                ddlNewsList.DataSource = sctList;
                ddlNewsList.DataBind();

                SiteContentType sct = sctList.FirstOrDefault(i => i.IsShortList == true);
                if (sct != null)
                {
                    ddlNewsList.SelectedValue = sct.SiteContentTypeId.ToString();
                }
            }
        }

        private void ClearShortListFlag()
        {
            List<SiteContentType> sctList = SiteContentType.GetAllContentTypes();
            if (sctList != null)
            {
                SiteContentType sct = sctList.FirstOrDefault(i => i.IsShortList == true);
                if (sct != null)
                {
                    sct.SetShortListProperty(false);
                }
            }
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ddlNewsList.SelectedValue))
            {
                ClearShortListFlag();

                SiteContentType sct = SiteContentType.Get(Convert.ToInt32(ddlNewsList.SelectedValue));
                if (sct != null)
                {
                    sct.SetShortListProperty(true);
                }
            }
            else
            {
                ClearShortListFlag();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillContentTypeList();
            }
        }
    }
}